Basics of R

Welcome to the preliminary basics tutorial for R! In this tutorial, we’ll cover some fundamental concepts and operations in the R programming language. Once you have R and RStudio installed, open RStudio and follow along with the following basics.

R Console

The RStudio interface consists of several panels, including the console, script editor, environment, and more. The console is where you can directly interact with R by typing commands and executing them.

Basic Arithmetic Operations

Let’s start with some basic arithmetic operations:

# Addition
2 + 3
[1] 5
# Subtraction
5 - 2
[1] 3
# Multiplication
4 * 6
[1] 24
# Division
10 / 2
[1] 5

Assigning Variables

You can assign values to variables using the assignment operator <-:

# Assigning values to variables
x <- 5
y <- 3

# Using variables in calculations
result <- x + y

Data Structures

R supports several data structures, including vectors, matrices, data frames, and lists.

Vectors

Vectors are the simplest type of data structure in R. They contain items of the same type (numeric, character, or logical). You can create a vector using the c() function.

# Numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)
numeric_vector
[1] 1 2 3 4 5
# Character vector
character_vector <- c("apple", "banana", "cherry")
character_vector
[1] "apple"  "banana" "cherry"
# Logical vector
logical_vector <- c(TRUE, FALSE, TRUE)
logical_vector
[1]  TRUE FALSE  TRUE

You can access vector elements using square brackets [].

# Access the third element of numeric_vector
numeric_vector[3]
[1] 3

Matrices

Matrices are two-dimensional data structures where every element has the same type. You can create a matrix using the matrix() function.

# Create a 3x3 matrix
my_matrix <- matrix(1:9, nrow=3, ncol=3)
my_matrix
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

You can access matrix elements using square brackets [,].

# Access the element in the second row, third column
my_matrix[2, 3]
[1] 8

Data Frames

Data frames are used to store tabular data. They are similar to matrices but can contain different types of data in each column. You can create a data frame using the data.frame() function.

# Create a data frame
my_data_frame <- data.frame(
  Name = c("Alice", "Bob", "Charlie"),
  Age = c(24, 30, 28),
  Salary = c(50000, 60000, 55000)
)
my_data_frame
     Name Age Salary
1   Alice  24  50000
2     Bob  30  60000
3 Charlie  28  55000

You can access data frame elements using $ for columns or [,] for specific elements.

# Access the 'Age' column
my_data_frame$Age
[1] 24 30 28
# Access the element in the second row of the 'Name' column
my_data_frame[2, "Name"]
[1] "Bob"

Lists

Lists are R’s most complex data structure. They can contain elements of different types, including numbers, strings, vectors, and even other lists. You can create a list using the list() function.

# Create a list
my_list <- list(
  Name = "Alice",
  Age = 24,
  Hobbies = c("Reading", "Cycling", "Hiking")
)
my_list
$Name
[1] "Alice"

$Age
[1] 24

$Hobbies
[1] "Reading" "Cycling" "Hiking" 

You can access list elements using $ or [[ ]].

# Access the 'Hobbies' element
my_list$Hobbies
[1] "Reading" "Cycling" "Hiking" 
# Access the second hobby
my_list$Hobbies[2]
[1] "Cycling"

Functions

R provides numerous built-in functions for performing various tasks. Let’s use the mean() function as a very simple example to calculate the mean of the numeric vector we created previously:

mean(numeric_vector)
[1] 3

Conclusion

In this tutorial, we’ve covered some preliminary basics of R, including arithmetic operations, variable assignment, data structures, and functions. These concepts provide a foundation for further exploration of R programming.

For more in-depth learning, consider exploring additional resources such as online tutorials, books, and documentation.

Happy coding with R!